#include "GameEngine.h"

void GameEngine::release()
{
}

void GameEngine::initialize()
{
	state = MENU;
	// game running
	running = true;
	// in game menu
	igmenOn = false;
	// is game paused
	paused = false;

	// instance of engine
	pEngine = DX9Engine::getInstance();

	// get instance input
	pInput = Input::getInstance();

	// set initial text display color
	pEngine->setTextColor(D3DCOLOR_ARGB(255, 0, 0, 0));

	bg.initialize(L"images/background.png", V2DF(SCREEN_WIDTH*0.5f,SCREEN_HEIGHT*0.5f),
		0.0f, 1.0f, true);
#ifndef FULLSCREEN_MODE
	bg.setScale(0.57f);
#endif
	p.initialize(L"images/test.png", V2DF(100.0f, 100.0f), 1.0f, 0.0f);
	// initializes user interfaces and all it's textures
	ui.initialize();

#ifdef MYAPP_DEBUG_TRACE
	// set all mbdown and kbdown to false
	for(int i=0; i<MAX_MOUSEBUT; ++i) {
		mbdown[i] = false;
	}
	for(int i=0; i<MAX_KEYS; ++i) {
		kbdown[i] = false;
	}
	// initialize on screen display
	osd.initialize();
#endif
}

// Update GameEngine
void GameEngine::update(float dT)
{	// get UI state
	// influences game sate
	updateState();
	switch(state)
	{
	case MENU:
		ui.update();
		break;
	case GAME:
		if(pInput->keyPressed(DIK_SPACE))
			gamestart = true;

		// if game started
		// run game elements
		if(gamestart && !igmenOn)
		{
			// player update
			p.update(dT);
		}

		// User Interface
		if(igmenOn)
			ui.update();

		// If game resumed
		// turn off InGame menu
		if(ui.isResumed())
			igmenOn = false;

		// open/close InGame menu
		// if InGame menu is open
		// 'Pause' the game
		if(pInput->keyPressed(DIK_ESCAPE))
		{
			if(igmenOn)
			{
				igmenOn = false;
				DbgPrintf("Game resume\n");
				paused = false;
			}
			else
			{
				igmenOn = true;
				DbgPrintf("Game paused\n");
				paused = true;
			}
		}

		break;
	case WIN:
		break;
	case LOSE:
		break;
	}
#ifdef MYAPP_DEBUG_TRACE
	// checks for input and displays on console
	for(int i=0; i<MAX_MOUSEBUT; ++i) {
		if(pInput->mouseButtonDown(i) && !mbdown[i]) {
			mbdown[i] = true;
			DbgPrintf("Input > MouseButton[%d] > Down\n", i);
		} else {
			if(!pInput->mouseButtonDown(i) && mbdown[i]) {
				mbdown[i] = false;
				DbgPrintf("Input > MouseButton[%d] > Released\n", i);	
			}
		}
	}
	for(int i=0; i<MAX_KEYS; ++i) {
		if(pInput->keyDown(i) && !kbdown[i]) {
			kbdown[i] = true;
			DbgPrintf("Input > Keyboard[%d] > Down\n", i);
		} else {
			if(!pInput->keyDown(i) && kbdown[i]) {
				kbdown[i] = false;
				DbgPrintf("Input > Keyboard[%d] > Released\n", i);
			}
		}
	}
	// debug screen display
	osd.update(dT);
#endif
}

// Render GameEngine
void GameEngine::render()
{
	bg.draw();
	switch(state)
	{
	case MENU:
		ui.render();
		break;
	case GAME:
		p.render();
		if(igmenOn)
		{
			ui.render();
		}
		break;
	case WIN:
		break;
	case LOSE:
		break;
	}
}

void GameEngine::renderText()
{
	switch(state)
	{
	case MENU:
		ui.drawText();
		break;
	case GAME:
		if(!gamestart)
		{
			char text[20];
			sprintf(text, "Press Spacebar.\n");
			RECT temp;
			SetRect(&temp, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
			pEngine->writeTextCenter(text, temp);
		}
		else	
		{
			if(igmenOn)
				ui.drawText();
		}
		break;
	case WIN:
		break;
	case LOSE:
		break;
	}
#ifdef MYAPP_DEBUG_TRACE
	// draws debug info to screen
	osd.renderText();
#endif
}